﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Status
    {
        #region Properties
        public Language Language
        {
            get
            {
                return this.GetLanguage();
            }
        }
        #endregion

        #region Methods
        public Language GetLanguage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from Languages in _db.Languages
                    where Languages.ID == this.ID_Language
                    select Languages;
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static List<Status> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from status in _db.Status
                    select status;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Status status)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (status.ID_Language < 1)
                errorList.Add(new ErrorItem("ID_Language", "Language is required."));
            if (status.Description == "")
                errorList.Add(new ErrorItem("Description", "Description is required."));
            if (status.Description.Length > 300)
                errorList.Add(new ErrorItem("Description", "Description is only allow up to 300 characters."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Status status)
        {
            List<ErrorItem> errorList = InputInfomationValidation(status);
            if (errorList.Count == 0)
            {
                System.Object ob = (System.Object)status;
                if (!Provider.Insert(ref ob, "Status"))
                    errorList.Add(new ErrorItem("Insert", "Failed."));
            }
            return errorList;
        }
        #endregion

    }
}